package service;

import model.Session;
import model.Hall;
import util.LogUtil;

import java.util.*;

public class SessionService {
    private List<Session> sessions = new ArrayList<>();
    private HallService hallService;

    public SessionService(HallService hallService) {
        this.hallService = hallService;
    }

    public void sessionManageMenu(Scanner sc) {
        System.out.println("1. 列出所有场次 2. 添加场次 3. 删除场次 4. 修改场次 5. 返回");
        int op = sc.nextInt();
        switch (op) {
            case 1:
                for (Session s : sessions) {
                    System.out.println("ID:" + s.getId() + " 片名:" + s.getMovieName() + " 影厅:" + s.getHallName() + " 时间:" + s.getTime() + " 价格:" + s.getPrice());
                }
                break;
            case 2:
                System.out.print("片名: ");
                String movieName = sc.next();
                System.out.print("影厅: ");
                String hallName = sc.next();
                Hall hall = hallService.getHallByName(hallName);
                if (hall == null) {
                    System.out.println("影厅不存在！");
                    return;
                }
                System.out.print("时间(如2024-06-01 19:00): ");
                String time = sc.next();
                System.out.print("价格: ");
                double price = sc.nextDouble();
                sessions.add(new Session(movieName, hallName, time, price, hall.getRows(), hall.getCols()));
                LogUtil.log("添加场次", movieName);
                System.out.println("添加成功！");
                break;
            case 3:
                System.out.print("输入要删除的场次ID: ");
                int delId = sc.nextInt();
                sessions.removeIf(s -> s.getId() == delId);
                LogUtil.log("删除场次", String.valueOf(delId));
                System.out.println("删除成功！");
                break;
            case 4:
                System.out.print("输入要修改的场次ID: ");
                int mid = sc.nextInt();
                for (Session s : sessions) {
                    if (s.getId() == mid) {
                        System.out.print("新片名: ");
                        s.setMovieName(sc.next());
                        System.out.print("新影厅: ");
                        String nhall = sc.next();
                        Hall nh = hallService.getHallByName(nhall);
                        if (nh == null) {
                            System.out.println("影厅不存在！");
                            return;
                        }
                        s.setHallName(nhall);
                        System.out.print("新时间: ");
                        s.setTime(sc.next());
                        System.out.print("新价格: ");
                        s.setPrice(sc.nextDouble());
                        LogUtil.log("修改场次", String.valueOf(mid));
                        System.out.println("修改成功！");
                        return;
                    }
                }
                System.out.println("未找到场次！");
                break;
            case 5:
                return;
        }
    }

    public List<Session> getSessions() {
        return sessions;
    }

    public Session getSessionById(int id) {
        for (Session s : sessions) {
            if (s.getId() == id) return s;
        }
        return null;
    }
}